package org.infodavid.common.model;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import org.infodavid.common.IBuilder;
import org.infodavid.common.ejb.UserSessionBean;

/**
 * The Class UserSessionTest.
 */
public class UserSessionTest extends AbstractBeanTest<IUserSession> {

	/** The user test. */
	private final UserTest userTest = new UserTest(this);

	/**
	 * Instantiates a new test.
	 * @param mainTest the main test
	 */
	@SuppressWarnings("rawtypes")
	public UserSessionTest(final AbstractBeanTest mainTest) {
		super(mainTest);
	}

	/**
	 * Instantiates a new user test.
	 */
	public UserSessionTest() {
		super();
	}

	/**
	 * Populate.
	 * @param count the count
	 * @return the collection< test data>
	 * @throws Exception the exception
	 */
	@Override
	protected List<IUserSession> doCreate(final int count) throws Exception {
		final IBuilder<IUserSession> builder =
		    ModelFactory.getInstance().getBuilder(IUserSession.class);
		final IUser user = userTest.populate(1).get(0);
		final List<IUserSession> items = new ArrayList<IUserSession>(count);
		IUserSession item;
		final DateTime now = new DateTime();
		long key;

		for (int i = 0; i < count; i++) {
			item = builder.create();
			key = System.nanoTime();

			item.setArchived(false);
			item.setAccessesCount(RAND.nextInt(1000));
			item.setAuthenticationServer("server-" + key);
			item.setAuthenticationSource("source-" + key);
			item.setUser(user);
			item.setKey(java.util.UUID.nameUUIDFromBytes(
			    (item.getUser().getLoginName() + System.nanoTime()).getBytes()).toString());
			item.setLastAccessDate(now);
			item.setLastIP(RAND.nextInt(255) + "." + RAND.nextInt(255) + '.' + RAND.nextInt(255) + '.'
			    + RAND.nextInt(255));

			items.add(item);
		}

		return items;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityClass()
	 */
	@Override
	public Class<UserSessionBean> getEntityClass() {
		return UserSessionBean.class;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityName()
	 */
	@Override
	public String getEntityName() {
		return IUserSession.class.getSimpleName();
	}
}
